import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class DeepWindow(QDialog):
    deep_Signal=pyqtSignal(str)
    def __init__(self):
        super(DeepWindow, self).__init__()
        self.setWindowTitle('设置深度')
        self.main_layout=QHBoxLayout()
        self.deep_label=QLabel('深度:')
        self.deep_edit=QLineEdit()
        rex=QRegExp("[0-9\.]+$")
        rv=QRegExpValidator(rex,self)
        self.deep_edit.setValidator(rv)
        self.unit_label=QLabel('/m')
        self.OK_btn=QPushButton('确认')
        self.OK_btn.clicked.connect(self._ok)
        self.main_layout.addWidget(self.deep_label)
        self.main_layout.addWidget(self.deep_edit)
        self.main_layout.addWidget(self.unit_label)
        self.main_layout.addWidget(self.OK_btn)
        self.setLayout(self.main_layout)

    def closeEvent(self, QCloseEvent):
        deep=self.deep_edit.text()
        if deep:
            self.deep_Signal.emit(deep)
        else:
            self.deep_Signal.emit('0')

    def _ok(self):
        deep=self.deep_edit.text()
        if deep:
            self.deep_Signal.emit(deep)
        else:
            self.deep_Signal.emit('0')
        self.close()

    def keyPressEvent(self, QKeyEvent):
        if QKeyEvent.key()==Qt.Key_Enter or QKeyEvent.key()==Qt.Key_Return:
            self._ok()
            self.close()

if __name__ == '__main__':
    app=QApplication(sys.argv)
    d=DeepWindow()
    d.show()
    sys.exit(app.exec_())